package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Attendence1;
import com.woniu.entity.Statement1;
import com.woniu.pojo.*;
import com.woniu.pojo.Class;
import com.woniu.service.ClassService;
import com.woniu.service.StatementService;
import com.woniu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller

@RequestMapping("statement")
public class StatementController {
    @Autowired
    StatementService statementService;
    @Autowired
    StudentService studentService;
    @Autowired
    ClassService classService;

    @RequestMapping("listJump")
    public String listJump(Model model){
        List<Student> studentList = studentService.selectAll();
        List<Class> classList = classService.selectAll();
        model.addAttribute("studentList",studentList);
        model.addAttribute("classList",classList);
        return "WEB-INF/statement/list";
    }
    //增加页面跳转
    @RequestMapping("addJump")
    public String addJump(Model model){
        List<Student> studentList = studentService.selectAll();
        List<Class> classList = classService.selectAll();
        model.addAttribute("studentList",studentList);
        model.addAttribute("classList",classList);
        return "WEB-INF/statement/add";
    }
    //增加修学复学数据
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Statement statement){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            statementService.add(statement);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //按住键删除休学复学数据
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer statementId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            statementService.deleteById(statementId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //查到一个复学休学数据
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String, Object> findOne(Integer statementId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            Statement statement =statementService.findOne(statementId);
            map.put("statement",statement);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //修改休学复学状态
    @RequestMapping("updateStatusById")
    @ResponseBody
    public Map<String, Object> updateStatusById(Integer statementId){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
           String str= statementService.updateStatusById(statementId);
            map.put("status",str);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //跳转到修改
    @RequestMapping("updateJump")
    public String updateJump(Model model, Integer statementId){
        List<Student> studentList = studentService.selectAll();
        List<Class> classList = classService.selectAll();
        model.addAttribute("studentList",studentList);
        model.addAttribute("classList",classList);
        Statement statement=statementService.findOne(statementId);
        model.addAttribute("statement",statement);
        model.addAttribute("statementId",statementId);
        return "WEB-INF/statement/update";
    }
    //修改
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String, Object> updateById(Statement statement){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            statementService.updateById(statement);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //考勤查询分页
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Statement1>  statementList=statementService.findAll1();
            PageInfo<Statement1> pageInfo=new PageInfo<Statement1>(statementList);
            map.put("pageInfo",pageInfo);
            map.put("statementList",statementList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    //考勤批量删除
    @RequestMapping("deleteByExample")
    @ResponseBody
    public Map<String,Object> deleteByExample(Integer[] statementIds){
        Map<String, Object> map=new HashMap<String,Object>();
        try {
            statementService.deleteByExample(Arrays.asList(statementIds));
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
    /*学生自己发起休学复学申请*/
    @RequestMapping("findDate")
    @ResponseBody
    public Map<String, Object> findDate(Date start, Date end,Statement statement, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="6") Integer pageSize){
        Map<String, Object> map=new HashMap<String,Object>();
        Map<String, Object> mapCondition=new HashMap<String,Object>();
        mapCondition.put("start",start);
        mapCondition.put("end",end);
        mapCondition.put("statement",statement);

        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Statement1>  statementList=statementService.findDate(mapCondition);
            PageInfo<Statement1> pageInfo=new PageInfo<Statement1>(statementList);
            map.put("pageInfo",pageInfo);
            map.put("statementList",statementList);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result",e.getMessage());
            e.printStackTrace();
        }
        return  map;
    }
}
